package com.lx.timer.springtask.config;

import com.lx.timer.springtask.TestQuartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置quartz
 *
 * @author 段誉
 * @create 2019-04-02 14:49
 */
@Configuration
public class QuartzConfig {
  @Bean
  public JobDetail teatQuartzDetail() {
    return JobBuilder.newJob(TestQuartz.class)
            .withIdentity("testQuartz").storeDurably().build();
  }

  @Bean
  public Trigger testQuartzTrigger() {
    //使用cron表达式
    CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
    //指定间隔时间
//    SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//            //设置时间周期单位秒，也可选择其他函数
//            .withIntervalInSeconds(3)
//            .repeatForever();
    return TriggerBuilder.newTrigger().forJob(teatQuartzDetail())
            .withIdentity("testQuartz")
            .withSchedule(cronScheduleBuilder)
            .build();
  }
}
